package apis

import (
	"fmt"
	"github.com/golang/protobuf/proto"
	"zinx/mmo_game_zinx/core"
	"zinx/mmo_game_zinx/pb"
	"zinx/ziface"
	"zinx/znet"
)

// WorldChatAPI 世界聊天的路由业务
type WorldChatAPI struct {
	znet.BaseRouter
}

func (wc *WorldChatAPI) Handle(request ziface.IRequest) {
	//解析客户端传递来的proto协议数据
	proto_msg := &pb.Talk{}
	err := proto.Unmarshal(request.GetData(), proto_msg)
	if err != nil {
		fmt.Println("talk msg unmarshal err!")
		return
	}
	//获取发送当前消息的player的ID
	pid, err := request.GetConnection().GetProperty("pid")
	if err != nil {
		fmt.Println("get playerID err!")
		return
	}
	//根据pid得到对应的player对象
	player := core.WorldManagerObj.GetPlayerByPid(pid.(int32))
	//将这个消息广播给其他所有在线的玩家
	player.Talk(proto_msg.Content)
}
